package com.xiaobingby.admin.sys.controller;


import com.xiaobingby.admin.sys.entity.Permission;
import com.xiaobingby.admin.sys.service.IPermissionService;
import com.xiaobingby.admin.sys.vo.MenuTreeVo;
import com.xiaobingby.admin.sys.vo.PermissionTreeVo;
import com.xiaobingby.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * sys_permission 权限表 前端控制器
 * </p>
 *
 * @author XiaoBingBy
 * @since 2018-10-29
 */
@Controller
@RequestMapping("/sys/permission")
public class PermissionController {

    @Autowired
    private IPermissionService iPermissionService;

    @ResponseBody
    @GetMapping("getPermissionTree")
    public R<List<PermissionTreeVo>> getPermissionTree() {
        List<PermissionTreeVo> permissionTree = iPermissionService.getPermissionTree();
        return new R<List<PermissionTreeVo>>().ok(permissionTree);
    }

    @ResponseBody
    @GetMapping("getRouter")
    public R<List<MenuTreeVo>> getRouter() {
        List<MenuTreeVo> userMenuTree = iPermissionService.getUserMenuTree();
        return new R<List<MenuTreeVo>>().ok(userMenuTree);
    }

    @ResponseBody
    @PostMapping
    public R addPermission(@RequestBody Permission permission) {
        boolean save = iPermissionService.save(permission);
        return R.ok(save);
    }

    @ResponseBody
    @PutMapping
    public R updatePermission(@RequestBody Permission permission) {
        boolean save = iPermissionService.updateById(permission);
        return R.ok(save);
    }

    @ResponseBody
    @DeleteMapping("{ids}")
    public R delPermission(@PathVariable Long[] ids) {
        boolean save = iPermissionService.removeByIds(Arrays.asList(ids));
        return R.ok(save);
    }

}
